package ypmits.utils {
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * @author Dave Lenz
	 */
	public class Aligner {

		public static function align( item : DisplayObject, square : Rectangle = null, stagealign : String = "BR", offset : Point = null, stage : Stage = null ) : void {
			var sw : Number;
			var sh : Number;
			
			if( !square ) {
				if( stage ) {
					sw = stage.stageWidth;
					sh = stage.stageHeight;
				} else {
					throw new Error( "ERROR! Since you didn't provide a square or a stage, you must set a square or a stage-parameter!" );
				}
			} else {
				sw = square.width;
				sh = square.height;
			}
			
			switch( stagealign ) {
				case StageAlign.BOTTOM_RIGHT:
					item.x = sw - offset.x;
					item.y = sh - offset.y;
					break;
				case StageAlign.BOTTOM_RIGHT:
				case StageAlign.BOTTOM:
					item.x = ( sw / 2) + ( item.width / 2 );
					item.y = sh - offset.y;
					break;
				case StageAlign.BOTTOM_RIGHT:
				case StageAlign.BOTTOM:
				case StageAlign.BOTTOM_LEFT:
					item.x = offset.x + item.width;
					item.y = sw - offset.y;
					break;
				case StageAlign.LEFT:
					item.x = offset.x + item.width;
					item.y = ( sh / 2) + ( item.height / 2 );
					break;
				case StageAlign.RIGHT:
					item.x = sw - offset.x;
					item.y = (sh / 2) + ( item.height / 2 );
					break;
				case StageAlign.TOP_RIGHT:
					item.x = sw - offset.x;
					item.y = offset.y + item.height;
					break;
				case StageAlign.TOP:
					item.x = ( sw / 2 ) + ( item.width / 2 );
					item.y = offset.y + item.height;
					break;
				case StageAlign.TOP_LEFT:
					item.x = offset.x + item.width;
					item.y = offset.y + item.height;
					break;
			}
		}
	}
}
